#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Generate problems

Created on Nov 26, 2011

@author: archpy
'''

from sys import argv
from time import strftime
import os

TEMPLATE = """#!/usr/bin/env python3
# -*- coding: utf-8 -*-
\"""
Print answer (Euler problem {version}) and process time

Created on {datetime}

@author: archpy
\"""

from time import time

def resolve(params):
    \"""
    Resolve Euler's problem {version}
    \"""
    return 0
    
if __name__ == "__main__":
    PARAMS = {{}}
    TIME_BEGIN = time()
    RES = resolve(PARAMS)
    TIME_END = time()-TIME_BEGIN
    print("Problem {version_full}:")
    print("------------")
    print("Answer : %d" %RES)
    print("Time : %f\\"" %TIME_END)"""

if __name__ == '__main__':
    CURDIR = os.path.dirname(__file__) or "."
    if not os.access(CURDIR, os.W_OK):
        raise IOError("%s is not writable" %(CURDIR))
    NUM = argv[1]
    if not NUM.isnumeric():
        raise ValueError("%s is nor an integer" % (NUM))
    NUM_FULL = NUM.zfill(4)
    DIRECTORY = os.path.join(CURDIR, "euler" + NUM_FULL)
    if (os.path.exists(DIRECTORY)):
        raise IOError("%s already exists" % (DIRECTORY))
    DATETIME = strftime("%a, %d %b %Y")
    CONTENT = TEMPLATE.format(version=NUM,
                              version_full=NUM_FULL,
                              datetime=DATETIME)
    os.mkdir(DIRECTORY)
    with open(os.path.join(DIRECTORY,"euler" + NUM_FULL + ".py"),"w") as f:
        f.write(CONTENT)
    with open(os.path.join(DIRECTORY,"__init__.py"),"w") as f:
        f.write("")
    print("OK")
